﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHDay_Linq.Domain;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
  
namespace NHDay_Linq
{  
	[TestFixture]
	public class T02_T06_ExtendingLinqTests : TestBase
	{
		[TestFixtureSetUp]
		public void FixtureSetup()
		{
			CreateDatabase();
		}

		[Test]
		public void T01_QueryWithOperatorInt64()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var magnus = session.Query<Person>().Where(t => t.Long == People[0].Long).FirstOrDefault();

				Assert.IsNotNull(magnus);
				Assert.AreEqual("Magnus", magnus.FirstName);

				tx.Commit();
			}
		}

		[Test]
		public void T02_QueryWithEqualsInt64()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var magnus =  session.Query<Person>().Where(t => t.Long.Equals(People[0].Long)).FirstOrDefault();
				
				Assert.IsNotNull(magnus);
				Assert.AreEqual("Magnus", magnus.FirstName);

				tx.Commit();
			}
		}

		[Test]
		public void T03_QueryWithOperatorGuid()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var magnus = session.Query<Person>().Where(t => t.Guid == People[0].Guid).FirstOrDefault();

				Assert.IsNotNull(magnus);
				Assert.AreEqual("Magnus", magnus.FirstName);

				tx.Commit();
			}
		}

		[Test]
		public void T04_QueryWithEqualsGuid()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var magnus = session.Query<Person>().Where(t => t.Guid.Equals(People[0].Guid)).FirstOrDefault();

				Assert.IsNotNull(magnus);
				Assert.AreEqual("Magnus", magnus.FirstName);

				tx.Commit();
			}
		}

		[Test]
		public void T05_QueryWithOperatorPerson()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var magnus = session.Query<Person>().Where(t => t == People[0]).FirstOrDefault();

				Assert.IsNotNull(magnus);
				Assert.AreEqual("Magnus", magnus.FirstName);

				tx.Commit();
			}
		}

		[Test]
		public void T06_QueryWithEqualsPerson()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var magnus = session.Query<Person>().Where(t => t.Equals(People[0])).FirstOrDefault();

				Assert.IsNotNull(magnus);
				Assert.AreEqual("Magnus", magnus.FirstName);

				tx.Commit();
			}
		}
	}
}
